<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\Area;
use app\common\model\ProductCate;
use app\common\model\Carousel;
use app\common\model\News;
use app\common\model\User;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Index extends Base
{
    public function index(): string
    {
        $cate = ProductCate::where(['pid' => 0, 'status' => 1])->field("id,icon,title")
            ->order('sort desc,id asc')->select()->toArray();
        View::assign([
            'carousel' => Carousel::getList(['position' => 1]),
            'news' => News::getList(),
            'cate' => array_chunk($cate, 8)
        ]);

        return View::fetch();
    }

    public function communal($type = 'register_msg'): string
    {
        $title = ['register_msg' => '注册协议', 'privacy_msg' => '隐私协议', 'company' => '关于我们'];
        View::assign([
            'title' => $title[$type],
            'content' => $this->system[$type]
        ]);

        return View::fetch();
    }

    public function getArea($id = 0, $level = 0): object
    {
        return Area::getArea($id, $level);
    }

    public function inquiryUser($code, $type = 'code'): Response
    {
        $user = User::where([$type => $code])->field('id,code,nickname,mobile,photo')->find();
        if (!$user) {
            return Result::fail('会员不存在');
        }

        return Result::success('success', $user);
    }
}